<template>
  <view class="home">
    <view class="banner">
      <view class="bannerParent">
        <view class="one">为家人申请</view>
        <view class="two">全家健康才是真安心</view>
        <view class="icon">
          <img
            src="https://wanhuhealth.oss-cn-beijing.aliyuncs.com/wanhuhealth-small-program/wanhu-mini-welfare/welfare/images/formBanner.png"
          />
        </view>
      </view>
    </view>
    <view>
      <form @submit="formSubmit">
        <view class="form">
          <view :class="['content marginTop-40 paddingBotNone']">
            <view class="title">为谁加入万户健康项目</view>
            <view class="disBlockBox">
              <view
                v-for="(item, index) in identityItem"
                :key="item.value"
                class="disBlockKid"
                @click="choseUser(item)"
              >
                <span :class="['disBlock', 'userIcon', index % 2 === 0 ? 'orgColor' : '']">
                  <img :src="item.icon" />
                  <span class="cur">
                    <img :src="item.value === formData.identity ? pCheckedIcon : pNoCheckedIcon" />
                  </span>
                </span>
                <text
                  :class="[
                    'disBlock',
                    'marginTB20',
                    'wordTitle',
                    item.value === formData.identity ? 'wordColorBlack' : ''
                  ]"
                  >{{ item.name }}</text
                >
              </view>
            </view>
            <view
              v-if="formData.identity === 2 || formData.identity === 3 || formData.identity === 4"
              class="userHistory"
            >
              历史记录：<view
                v-for="item in historyList"
                :key="item.memberId"
                :class="[item.memberId === historyCur ? 'spans historyCur' : 'spans']"
                @click="getUserInfo(item)"
                >{{ item.memberName }}</view
              >
              <view :class="[!historyCur && historyCur !== 0 ? 'spans historyCur' : 'spans']" @click="addUser"
                >新增+</view
              >
            </view>
            <view class="inputBox marginTop20">
              <span class="name lineHeight86">姓名</span>
              <input
                v-model="formData.name"
                class="uni-input lineHeight86"
                name="name"
                placeholder="请输入姓名"
                :disabled="isAuthen"
                placeholder-class="phcolor"
              />
              <!-- <view v-show="formData.name && !isAuthen" class="clearInp" @click.stop="clearInpVal('name')"
              ><img :src="closeIcon"
            /></view> -->
              <view class="clear"></view>
            </view>
            <view class="inputBox">
              <span class="name lineHeight86">身份证</span>
              <input
                v-model="formData.idCard"
                type="idcard"
                class="uni-input lineHeight86"
                name="idCard"
                placeholder="请输入身份证号"
                placeholder-class="phcolor"
                :disabled="isAuthen"
                @change="idCardChange($event, formData.idCard)"
              />
              <!-- <view v-show="formData.idCard && !isAuthen" class="clearInp" @click.stop="clearInpVal('idCard')"
              ><img :src="closeIcon"
            /></view> -->
              <view v-show="formData.idCard && isAuthen" class="idAuthen" @click.stop="clearInpVal('idCard')"
                >实名</view
              >
              <view class="clear"></view>
            </view>
            <view class="inputBox">
              <span class="name lineHeight86">手机号</span>
              <input
                v-model="formData.phone"
                type="number"
                class="uni-input lineHeight86"
                name="phone"
                :disabled="formData.identity === 1"
                placeholder="请输入手机号"
                placeholder-class="phcolor"
              />
              <!-- <view v-show="formData.phone && formData.identity !== 1" class="clearInp" @click.stop="clearInpVal('phone')"
              ><img :src="closeIcon"
            /></view> -->
              <view class="clear"></view>
            </view>
          </view>
          <view class="content">
            <!-- <cityCom ref="childChoseCity" :choseCityProps="choseCity" @getPropsAddress="getPropsAddress"></cityCom> -->
            <!-- <areaCom
            v-if="initAddress"
            ref="childChoseCity"
            :choseCityPropsInit="formData.address"
            @getPropsAddress="getPropsAddress"
          ></areaCom> -->
            <view class="name marginBot20">请选择地区</view>
            <areaCom
              :address="addressComData"
              :setPlaceholderClass="setAddressClass"
              :setValueClass="setValueClass"
              @confirm="getPropsAddress"
            ></areaCom>
            <!-- <view class="tipsDesc">保障计划已向：安徽芜湖、山西太原、贵州黔南州等地提供服务。</view> -->
          </view>
          <view class="content">
            <view class="title name fontSizeNone">身体状况如何</view>
            <view class="sickBox">
              <view
                v-for="item in sickItem"
                :key="item.value"
                :class="[item.value === formData.sick ? 'chose sickBtn' : 'sickBtn']"
                @click="choseSick(item.value)"
              >
                {{ item.name }}
                <view v-show="item.value === formData.sick" class="cur"><img :src="curIcon" /></view>
              </view>
            </view>
            <view v-if="formData.sick === 2" class="choseSickList">
              <view
                v-for="item in sickInitList"
                :key="item.id"
                :class="[item.active ? 'cur spans' : 'spans']"
                @click="getSickList(item)"
                >{{ item.name }}</view
              >
              <view v-for="item in sickList" :key="item.id" class="spans blackBg">
                {{ item.name }}
                <view class="clearSick" @click="clearSick(item.id)"><img :src="closeIcon" /></view>
              </view>
              <view class="spans" @click="otherSickBtn">其他疾病 > </view>

              <view class="tips">提示：所患病症将影响后续保障服务，请务必如实填写</view>
            </view>
          </view>
        </view>
        <fixed-bottom-button>
          <view class="uni-btn-v">
            <button form-type="submit">提交</button>
          </view>
        </fixed-bottom-button>
      </form>
    </view>
    <view v-if="sickVisible" class="popDom">
      <sickListPage
        :sickAllList="sickAllList"
        :listData="choseIllList"
        @popClose="popClose"
        @popCenter="popCenter"
        @choseSickSearch="choseSickSearch"
      ></sickListPage>
    </view>
  </view>
</template>
<script>
import sickListPage from '@/components/sickListPageLoad';
import { hospitalId, registerSource } from '@/common/baseUrl.js';
import { getPatientId } from '@/utils/auth.js';
import { getData, getInfoByIdCard } from '@/utils/optimize.js';
// import cityCom from '@/components/cityCom';
// import areaCom from '@/components/areaCom';
import areaCom from '@/components/addressCom';
import fixedBottomButton from '@/components/fixedBottomBtn';
import { addPhysicalExamination } from '@/api/assessQuestion';
// 引入统计
import { tongji } from '@/utils/tools.js';

import {
  getUserInfoByPatientId,
  getFamilyMember,
  getPatientIdByIdCard,
  getRealNameResult,
  sendUserInfoForm,
  bindPatients,
  getDiseasesHotList,
  addFamilyMember,
  addMedical,
  checkRealName
  // getDiseasesList
} from '@/api/join.js';

import pCheckedIcon from '@/static/icon/p-checked.png';
import pNoCheckedIcon from '@/static/icon/p-nochecked.png';

let graceChecker = require('../../common/graceChecker.js');
export default {
  components: {
    sickListPage,
    areaCom,
    fixedBottomButton
    // cityCom
  },
  data() {
    return {
      curIcon:
        'https://wanhuhealth.oss-cn-beijing.aliyuncs.com/wanhuhealth-small-program/wanhu-mini-welfare/welfare/icon/cur.png',
      pCheckedIcon,
      pNoCheckedIcon,
      closeIcon:
        'https://wanhuhealth.oss-cn-beijing.aliyuncs.com/wanhuhealth-small-program/wanhu-mini-welfare/welfare/icon/close.png',
      identityItem: [
        {
          name: '本人',
          value: 1,
          icon:
            'https://wanhuhealth.oss-cn-beijing.aliyuncs.com/wanhuhealth-small-program/wanhu-mini-welfare/welfare/index/man.png',
          click: false
        },
        {
          name: '父母',
          value: 3,
          icon:
            'https://wanhuhealth.oss-cn-beijing.aliyuncs.com/wanhuhealth-small-program/wanhu-mini-welfare/welfare/index/parent.png',
          click: false
        },
        {
          name: '配偶',
          value: 2,
          icon:
            'https://wanhuhealth.oss-cn-beijing.aliyuncs.com/wanhuhealth-small-program/wanhu-mini-welfare/welfare/index/woman.png',
          click: false
        },
        {
          name: '子女',
          value: 4,
          icon:
            'https://wanhuhealth.oss-cn-beijing.aliyuncs.com/wanhuhealth-small-program/wanhu-mini-welfare/welfare/index/child.png',
          click: false
        }
      ],
      historyItem: {
        spouseItem: [],
        parentItem: [],
        childrenItem: []
      },
      historyList: [],
      historyCur: '',
      sickItem: [
        {
          name: '未确诊慢病',
          value: 1
        },
        {
          name: '已确诊慢病',
          value: 2
        }
      ],
      sickList: [],
      // formData: {
      //   identity: '', // 为谁加入
      //   name: '', // 姓名
      //   idCard: '', // 身份证
      //   phone: '', // 手机号
      //   sick: '', // 是否患病
      //   sickList: [], // 患病了，都有什么病
      //   address: [10010, 10031]
      // },
      formData: this.$store.getters.storeFormData,
      isAuthen: false,
      ownInfo: {},
      sickVisible: false,
      sickInitList: [],
      sickAllList: [],
      searchChoseList: [],
      clickChoseList: [],
      choseIllList: [],
      loginPatientId: getPatientId(),
      submitPatientId: '',
      initAddress: true,
      otherPhone: undefined,
      ownPhone: '',
      familyUserData: {},
      setAddressClass: 'colorGrey',
      setValueClass: 'colorGreyDep',
      addressComData: [] // 省市区组件使用格式，提交信息的时候需要转为对象格式
    };
  },
  onLoad(options) {
    this.getUserData();
    this.getHistoriesUser();
    this.getDiseasesListObj();
    if (options && options.fromPage === '1') {
      this.formData.identity = '';
      this.addUser();
    }
    // 区分疾病，热门疾病和搜索列表中的疾病
  },
  methods: {
    getUserData() {
      const self = this;
      const patientId = wx.getStorageSync('patientid');
      getUserInfoByPatientId(patientId).then((res) => {
        if (res.code === 0 && res.data) {
          self.ownInfo = res.data;
          // 判断登录人男女，为谁加入那展示不同图标，本人和配偶男女更换
          if (res.data.idCard) {
            // 1男，0女
            let sex = getInfoByIdCard(res.data.idCard, 2);
            if (sex === 0) {
              [this.identityItem[0].icon, this.identityItem[2].icon] = [
                this.identityItem[2].icon,
                this.identityItem[0].icon
              ];
            }
          }
        }
      });
    },
    filterDisease(val) {
      // 匹配有没有热门疾病数据 sick
      let sick = this.sickInitList.filter((item) => this.formData.diseases.some((ele) => ele.id === item.id));
      // 匹配除了热门疾病的数据 直接展示在列表
      let sickDelete = this.formData.diseases.filter((item) => !sick.some((ele) => item.id === ele.id));
      this.sickInitList.filter((item) => {
        item.active = sick.some((ele) => {
          return item.id === ele.id;
        });
        return item;
      });
      this.clickChoseList = sick.map((item) => {
        return {
          id: item.id,
          icd10: item.icd10
        };
      });
      this.searchChoseList = sickDelete.map((item) => {
        return {
          id: item.id,
          icd10: item.icd10
        };
      });
      this.sickList = sickDelete;
    },
    getOwnData() {
      const self = this;
      this.initAddress = false;
      this.formData.name = this.ownInfo.name;
      this.formData.idCard = this.ownInfo.idCard;
      this.formData.phone = this.$store.getters.phone || this.ownInfo.phone;
      this.formData.address = this.ownInfo.address;
      if (this.formData.address.cityId) {
        this.addressComData = this.addressFormatTrsf(this.ownInfo.address);
      } else {
        this.addressComData = [];
      }

      this.otherPhone = this.ownInfo.machineNumber || undefined;
      this.ownPhone = this.ownInfo.phone || undefined;
      if (this.ownInfo.realNameAuthenticationPassed && this.ownInfo.realNameAuthenticationPassed === 1) {
        this.isAuthen = true;
      }
      if (this.ownInfo.diseases && this.ownInfo.diseases.length > 0) {
        self.formData.diseases = this.ownInfo.diseases;
        self.filterDisease();
        self.formData.sick = 2;
      } else {
        self.formData.sick = '';
      }
      setTimeout(function () {
        self.initAddress = true;
      }, 60);
    },
    choseSickSearch(val) {
      this.sickVisible = false;
      let indexs = '';
      this.sickList.forEach((item, index) => {
        if (item.id === val.id) {
          indexs = index;
          // this.sickList.splice(index, 1);
        }
      });
      // 判断默认展示疾病列表有没有，有的话给默认列表选中，没有的话追加
      let isInitSickList = false;
      this.sickInitList.forEach((item) => {
        if (item.id === val.id && !item.active) {
          this.getSickList(val);
          isInitSickList = true;
        }
      });
      if (isInitSickList) {
        return;
      }
      if (!indexs && indexs !== 0) {
        this.sickList.push(val);
      }
      this.searchChoseList = this.sickList.map((item) => {
        return {
          id: item.id,
          icd10: item.icd10
        };
      });
    },
    getSickList(val) {
      this.sickInitList.forEach((item) => {
        if (item.id === val.id) item.active = !item.active;
      });

      let filterData = this.sickInitList.filter((item) => item.active);

      this.clickChoseList = filterData.map((item) => {
        return {
          id: item.id,
          icd10: item.icd10
        };
      });
    },
    getDiseasesListObj() {
      getDiseasesHotList().then((res) => {
        if (res.code === 0 && res.data) {
          this.sickInitList = res.data;
        }
      });
    },
    popCenter() {
      this.sickVisible = false;
    },
    popClose() {
      this.sickVisible = false;
    },
    otherSickBtn() {
      const self = this;
      this.choseIllList = [...new Set([...this.searchChoseList, ...this.clickChoseList])];
      setTimeout(function () {
        self.sickVisible = true;
      }, 100);
    },
    async getUserInfo(item) {
      const self = this;
      this.initAddress = false;
      this.addUser();
      if (item && item.memberId) {
        // self.formData.idCard = item.memberId;
        self.formData.name = item.memberName;
        // self.formData.phone = item.insuredPhone;
      }
      this.historyCur = item.memberId;
      this.isAuthen = await getUserInfoByPatientId(item.memberId).then((res) => {
        if (res.code === 0) {
          self.formData.idCard = res.data.idCard;
          self.formData.phone = res.data.phone;
          self.formData.address = res.data.address;
          self.addressComData = self.addressFormatTrsf(res.data.address);
          if (res.data.address && res.data.address.cityId) {
            self.addressComData = this.addressFormatTrsf(res.data.address);
          } else {
            self.addressComData = [];
          }
          self.initAddress = true;
          self.formData.diseases = res.data.diseases;
          self.otherPhone = res.data.machineNumber;
          self.ownPhone = res.data.phone;
          if (self.formData.diseases && self.formData.diseases.length > 0) {
            self.filterDisease();
            self.formData.sick = 2;
          } else {
            self.formData.sick = '';
          }
        }
        if (res.code === 0 && res.data.realNameAuthenticationPassed === 1) {
          return true;
        }
        return false;
      });
    },
    async idCardChange(e, val) {
      const self = this;
      if (val && val.length === 18) {
        this.isAuthen = false;
        const data = {
          phone: '',
          idCard: this.formData.idCard
        };
        let patientId = await getPatientIdByIdCard(data).then((res) => {
          if (res.code === 0) {
            return res.data;
          } else {
            uni.showToast({ title: res.message, icon: 'none' });
          }
          return false;
        });
        if (patientId) {
          this.submitPatientId = patientId;
          getUserInfoByPatientId(patientId).then((res) => {
            if (res.code === 0) {
              self.formData.name = res.data.name || '';
              self.ownPhone = res.data.phone || '';
              self.otherPhone = res.data.machineNumber || '';
              if (res.data.realNameAuthenticationPassed && res.data.realNameAuthenticationPassed === 1) {
                self.isAuthen = true;
              }
            }
          });
        }
      } else if (val.length > 0) {
        uni.showToast({ title: '请输入正确的身份证号', icon: 'none' });
      }
    },
    async getHistoriesUser() {
      const self = this;
      const patientId = wx.getStorageSync('patientid');
      await getFamilyMember(patientId).then((res) => {
        self.historyItem = {
          spouseItem: [],
          parentItem: [],
          childrenItem: []
        };
        // "relationShip":1,//关系 1本人 2配偶 3 父母 4 子女
        if (res.code === 0 && res.data) {
          this.familyUserData = res.data;
          res.data.forEach((item) => {
            if (item.relation === 2) {
              self.historyItem.spouseItem.push(item);
            }
            if (item.relation === 3) {
              self.historyItem.parentItem.push(item);
            }
            if (item.relation === 4) {
              self.historyItem.childrenItem.push(item);
            }
          });
        }
      });
    },
    // Monday Tuesday Wednesday Thursday Friday Saturday Sunday
    async choseUser(val) {
      let id = val.value;
      if (id === this.formData.identity) {
        return;
      }
      this.historyItem = {
        spouseItem: [],
        parentItem: [],
        childrenItem: []
      };
      this.historyList = [];
      await this.getHistoriesUser();
      this.addUser();
      this.isAuthen = false;
      this.historyCur = '';
      let getChoseItem = this.identityItem.find((item) => {
        return item.value === id;
      });
      this.formData.identity = getChoseItem.value;
      if (id === 1) {
        this.getOwnData();
      }
      if (id === 2) {
        this.historyList = this.historyItem.spouseItem;
      }
      if (id === 3) {
        this.historyList = this.historyItem.parentItem;
      }
      if (id === 4) {
        this.historyList = this.historyItem.childrenItem;
      }
      this.historyList = [...new Set(this.historyList)];
    },
    clearInpVal(e) {
      this.formData[e] = '';
    },
    addUser() {
      this.historyCur = '';
      let temporaryIndentity = this.formData.identity;
      this.formData = {
        identity: '', // 为谁加入
        name: '', // 姓名
        idCard: '', // 身份证
        phone: '', // 手机号
        sick: '', // 是否患病
        sickList: [], // 患病了，都有什么病
        address: [] // 城市[10010, 10031]
      };
      this.formData.identity = temporaryIndentity;
      // 触发城市子组件清空选中的城市
      // this.$refs.childChoseCity.clearChoseCity();
      this.addressComData = [];
      // 清空选中疾病
      this.searchChoseList = [];
      this.clickChoseList = [];
      this.sickList = [];
      this.sickInitList.map((item) => {
        item.active = false;
        return item;
      });

      this.isAuthen = false;
    },
    // 接收城市组件传递回来的数据
    getPropsAddress(address) {
      this.addressComData = address;
      const objData = {
        provinceId: address[0],
        cityId: address[1],
        areaId: address[2]
      };
      this.formData.address = objData;
      // this.formData.address = address;
    },
    // eslint-disable-next-line complexity
    async formSubmit(e) {
      // 友盟统计
      tongji('104419e9-bcfd-4f5e-8d8c-b6b0dc0e1428');
      // 判断给谁购买服务包
      if (!this.formData.identity) {
        uni.showToast({ title: '请选择为谁加入万户健康项目', icon: 'none' });
        return false;
      }
      // 定义表单规则
      let rule = [
        {
          name: 'name',
          checkType: 'string',
          checkRule: '1,4',
          errorMsg: '姓名应为1-4个字符'
        },
        {
          name: 'idCard',
          checkType: 'idcard',
          checkRule: '',
          errorMsg: '请填写正确身份证号'
        },
        {
          name: 'phone',
          checkType: 'phoneno',
          checkRule: '',
          errorMsg: '请填写正确手机号'
        }
      ];
      const getIllList = [...new Set([...this.searchChoseList, ...this.clickChoseList])] || [];
      if (this.formData.sick === 2 && getIllList.length < 1) {
        uni.showToast({ title: '请选择疾病', icon: 'none' });
        return;
      }
      // 购买人信息校验
      let formData = e.detail.value;
      let checkRes = graceChecker.check(formData, rule);
      if (!checkRes) {
        uni.showToast({ title: graceChecker.error, icon: 'none' });
        return false;
      }
      // 城市校验
      if (!this.formData.address.areaId) {
        uni.showToast({ title: '请选择所在城市', icon: 'none' });
        return false;
      }
      // 健康状况如何
      if (!this.formData.sick) {
        uni.showToast({ title: '请选择健康状况', icon: 'none' });
        return false;
      }
      // 校验必校项 end
      // 校验身份证信息是否可以通过
      let checkError = false;
      const noPatientInfo = {
        idCard: this.formData.idCard,
        name: this.formData.name
      };
      await checkRealName(noPatientInfo).then((res) => {
        if (res.code !== 0) {
          checkError = true;
          uni.showToast({ title: res.message, icon: 'none' });
        }
      });
      if (checkError) {
        return false;
      }
      // 校验用户身份信息是否可通过实名校验 start
      // ①先通过身份证获取patientId
      const userData = {
        phone: '',
        idCard: this.formData.idCard
      };
      const userDataPhone = {
        phone: this.formData.phone,
        idCard: ''
      };
      let newIdCardNoId = false;
      let newIdCardNoIdButIphoneHaveId = false;
      // let getPatientIdPhone = null;
      let patientId = await getPatientIdByIdCard(userData).then((res) => {
        if (res.code === 0) {
          // getPatientIdPhone = res.data.phone || undefined;
          return res.data;
        } else {
          uni.showToast({ title: res.message, icon: 'none' });
        }
        return false;
      });
      if (!patientId) {
        newIdCardNoId = true;
        // 如果身份证查不到，就用手机号查patientId
        patientId = await getPatientIdByIdCard(userDataPhone).then((phoneRes) => {
          if (phoneRes.code === 0) {
            if (phoneRes.data) {
              newIdCardNoIdButIphoneHaveId = true;
            }
            return phoneRes.data;
          } else {
            uni.showToast({ title: phoneRes.message, icon: 'none' });
          }
        });
      }
      // ①-b补充:如果没有patientId，去注册
      if (patientId === false) {
        return false;
      } else if (!patientId) {
        const bindPatientInfo = {
          phone: this.formData.phone,
          name: this.formData.name,
          idCard: this.formData.idCard,
          hospitalId: hospitalId,
          cid: uni.getStorageSync('cid') || '',
          source: registerSource
        };
        await bindPatients(bindPatientInfo).then((bindPatientsRes) => {
          if (bindPatientsRes.code === 0 && bindPatientsRes.data) {
            patientId = bindPatientsRes.data;
          } else {
            checkError = true;
            uni.showToast({
              title: bindPatientsRes.message,
              duration: 2000,
              icon: 'none'
            });
            return false;
          }
        });
      }
      if (checkError) {
        return false;
      }
      // 如果身份证没查到信息，手机号查到信息了，去注册没有手机号的新用户
      if (newIdCardNoIdButIphoneHaveId && newIdCardNoId) {
        patientId = await this.bindNewUserNoPhone();
      }
      if (!patientId) {
        return false;
      }
      // ②通过patientId，name，idCard来判断是否通过实名认证
      let realNameData = {
        patientId: patientId || '',
        name: this.formData.name,
        idCard: this.formData.idCard
      };
      if (this.formData.identity === 1) {
        realNameData.patientId = this.loginPatientId;
      }
      await getRealNameResult(realNameData).then((res) => {
        if (res.code === 0 || res.code === 2) {
          console.log('实名认证已通过', res);
        } else {
          checkError = true;
          wx.showModal({
            title: '认证失败',
            content: res.message,
            showCancel: false,
            success: function () {}
          });
          return false;
        }
      });
      if (checkError) {
        return false;
      }
      // 校验用户身份信息是否可通过实名校验 end
      // 判断手机号是否已存在或者与其他用户的手机号冲突 start
      const phoneData = {
        phone: this.formData.phone,
        idCard: ''
      };
      let machineNumber = '';
      let checkPhoneGetPatientId = '';
      uni.showToast({
        title: '加载中',
        icon: 'loading',
        duration: 15000,
        mask: true
      });
      await getPatientIdByIdCard(phoneData).then((res) => {
        if (res.code === 0) {
          checkPhoneGetPatientId = res.data;
          return res.data;
        } else {
          checkError = true;
          uni.showToast({ title: res.message, icon: 'none' });
        }
        return false;
      });
      if (checkError) {
        return false;
      }
      if (checkPhoneGetPatientId && checkPhoneGetPatientId !== patientId) {
        machineNumber = this.formData.phone;
      }
      await getUserInfoByPatientId(patientId).then((res) => {
        if (res.code === 0) {
          this.ownPhone = res.data.phone;
        }
      });
      if (this.ownPhone && this.ownPhone !== this.formData.phone) {
        machineNumber = this.formData.phone;
      }
      this.formData.patientId = patientId;
      // 判断该用户是否存在【服务包配置-项目】为“项目”且状态为“服务中”的服务订单 end
      let diseases = [...new Set([...this.searchChoseList, ...this.clickChoseList])].map((item) => item.id);
      console.log('疾病列表疾病列表', diseases);
      let sendData = {
        identity: this.formData.identity,
        sick: this.formData.sick || undefined,
        name: this.formData.name || undefined,
        idCard: this.formData.idCard || undefined,
        patientId: patientId || undefined,
        insurance: undefined,
        address: this.formData.address || undefined,
        noDisease: this.formData.sick === 1 ? 1 : 0,
        diseases: diseases, // 疾病['疾病id1','疾病id2']
        phone: machineNumber ? this.ownPhone : this.formData.phone,
        machineNumber: machineNumber || undefined
      };
      if (sendData.diseases.length < 1) {
        sendData.diseases = undefined;
      }
      // 如果选择了健康，疾病字段要清空
      if (sendData.sick === 1) {
        sendData.diseases = undefined;
      }
      // 如果没有其它联系方式，尝试读取之前有其它联系方式么
      if (!machineNumber) {
        sendData.machineNumber = this.otherPhone;
      }
      // 如果选择的不是自己，新添加家庭成员关系
      let isHaveFamily = this.familyUserData.some(
        (item) => item.memberId === patientId && item.relation !== this.formData.identity
      );
      if ((wx.getStorageSync('patientid') === patientId && this.formData.identity !== 1) || isHaveFamily) {
        uni.showToast({
          title: '服务对象与您的关系记录与已选关系标签不对应，请确认。',
          icon: 'none'
        });
        return false;
      }
      // if (this.formData.identity !== 1) {
      this.addFUser();
      // }
      // 新增客户就诊记录
      this.addMedicalObj();
      await sendUserInfoForm(sendData).then((res) => {
        uni.hideToast();
        if (res.code === 0) {
          this.$store.commit('join/SET_STOREFORMDATA', {
            ...sendData,
            ...{ sickList: getIllList }
          });
          this.$wxPromise.navigateTo({
            url: '/pages/join/plan'
          });
        } else {
          uni.showToast({ title: res.message, icon: 'none' });
        }
      });
    },
    async bindNewUserNoPhone() {
      const bindPatientInfo = {
        name: this.formData.name,
        idCard: this.formData.idCard,
        hospitalId: hospitalId,
        machineNumber: this.formData.phone,
        cid: uni.getStorageSync('cid') || '',
        source: registerSource
      };
      let patientId = '';
      await bindPatients(bindPatientInfo).then((bindPatientsRes) => {
        if (bindPatientsRes.code === 0 && bindPatientsRes.data) {
          patientId = bindPatientsRes.data;
        } else {
          uni.showToast({
            title: bindPatientsRes.message,
            duration: 2000,
            icon: 'none'
          });
          return false;
        }
      });
      return patientId;
    },
    addFUser() {
      const data = {
        patientId: this.loginPatientId,
        memberId: this.formData.patientId,
        relation: this.formData.identity
      };
      addFamilyMember(data);
    },
    addMedicalObj() {
      let icd10Data = [...new Set([...this.searchChoseList, ...this.clickChoseList])] || [];
      console.log('带有icd10的疾病', icd10Data);
      const isSick = this.formData.sick === 1 ? 0 : 1;
      const time = getData();
      const data = {
        patientId: this.formData.patientId,
        medicalDate: time,
        isSick: isSick // 是否患病  0=否 1=是
      };
      addMedical(data);
      const examData = {
        physicalExaminationDate: time,
        createBy: this.loginPatientId,
        isSick: isSick,
        source: registerSource,
        businessSource: 3,
        patientChronicDiseases: icd10Data.map((item) => {
          return { icd10: item.icd10 };
        })
      };
      addPhysicalExamination(this.formData.patientId, examData);
    },
    choseSick(id) {
      this.formData.sick = id;
    },
    clearSick(id) {
      const self = this;
      this.sickList.splice(
        self.sickList.findIndex((item) => item.id === id),
        1
      );
      this.searchChoseList.splice(
        self.sickList.findIndex((item) => item.id === id),
        1
      );
    },
    addressFormatTrsf(val) {
      let item = [];
      if (Object.prototype.toString.call(val) === '[object Object]' && val.cityId) {
        item = [val.provinceId, val.cityId, val.areaId];
        return item;
      }
      return val;
    }
  }
};
</script>
<style>
page {
  background-color: #f4f4f4;
}
.uni-btn-v button {
  border: none !important;
}
button:after {
  border: none !important;
}
.phcolor {
  color: rgba(0, 0, 0, 0.3);
}
</style>

<style lang="scss" scoped>
.home {
  padding-bottom: calc(88rpx + 40rpx + constant(safe-area-inset-bottom) + 60rpx);
  padding-bottom: calc(88rpx + 40rpx + env(safe-area-inset-bottom) + 60rpx);
  @supports not (constant(safe-area-inset-bottom)) {
    padding-bottom: 180rpx;
  }
}
.box {
  padding: 20px;
}
.banner {
  width: 750rpx;
  height: 380rpx;
  /* border: 2rpx solid rgba(0, 0, 0, 0); */
  /* background: linear-gradient(186deg, #fd8d8d 0%, #f63434 100%); */
  background: #f96a6a;
}
.bannerParent {
  position: relative;
  height: 100%;
  width: 100%;
}
.banner .one {
  font-size: 48rpx;
  font-weight: 500;
  color: rgba(255, 255, 255, 0.9);
  position: absolute;
  left: 50rpx;
  top: 78rpx;
  z-index: 9;
}
.banner .two {
  position: absolute;
  font-size: 56rpx;
  font-weight: bold;
  color: #ffe6b3;
  left: 50rpx;
  top: 150rpx;
  z-index: 9;
}
.banner .icon {
  position: absolute;
  right: 24rpx;
  top: 30rpx;
  width: 300rpx;
  height: 230rpx;
}
.banner .icon img {
  width: 300rpx;
  height: 230rpx;
}

.getPhoneBtn {
  color: #fff;
  width: 700rpx;
  height: 88rpx;
  line-height: 88rpx;
  border-radius: 24rpx;
  font-size: 36rpx;
  margin-top: 100rpx;
  background: -moz-linear-gradient(top, #fe894d 0%, #ef3931 100%);
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #fe894d), color-stop(100%, #ef3931));
  background: -webkit-linear-gradient(top, #fe894d 0%, #ef3931 100%);
  background: -o-linear-gradient(top, #fe894d 0%, #ef3931 100%);
  background: -ms-linear-gradient(top, #fe894d 0%, #ef3931 100%);
  background: linear-gradient(to bottom, #fe894d 0%, #ef3931 100%);
}
.disBlockBox {
  font-size: 28rpx;
  justify-content: space-between;
  flex-wrap: wrap;
  margin: 0 auto;
  text-align: center;
}
.disBlockBox .disBlockKid {
  display: inline-block;
  position: relative;
  width: 120rpx;
  margin: 0 22rpx;
  border-radius: 16rpx;
  margin-bottom: 26rpx;
}

.disBlockBox .disBlockKid:last-child {
  margin-right: 0;
}
.disBlockBox .disBlockKid:first-child {
  margin-left: 0;
}
.disBlockKid .cur {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 40rpx;
  height: 40rpx;
}
.disBlockKid .cur img {
  width: 100%;
  height: 100%;
}
.disBlockBox .disBlockKid .disBlock {
  display: inline-block;
  width: 100%;
  text-align: center;
}
.userIcon img {
  width: 120rpx;
  height: 120rpx;
  border-radius: 120rpx;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.userIcon {
  width: 120rpx;
  height: 120rpx;
  position: relative;
  background-color: #e5edfe;
  border-radius: 120rpx;
}
.orgColor {
  background: rgba(254, 236, 215, 1);
}
.marginTB20 {
  margin: 15rpx 0;
}
.content {
  padding: 40rpx;
  border-radius: 24rpx;
  background-color: #fff;
  margin: 24rpx 24rpx 24rpx 24rpx;
  /* box-shadow: 0px 12px 12px #e8e8e8; */
}
.marginTop-40 {
  margin-top: -120rpx;
}
.paddingBotNone {
  padding-bottom: 12rpx;
}
.content .title {
  font-size: 36rpx;
  margin-bottom: 40rpx;
}
.lineHeight86 {
  height: 86rpx;
  line-height: 86rpx;
}
.size28 {
  font-size: 28rpx;
}
.inputBox {
  border-bottom: 1px solid #e8e8e8;
  font-size: 28rpx;
  position: relative;
}
.inputBox:last-child {
  border: none;
}
.clearInp {
  position: absolute;
  right: 0;
  top: 25rpx;
  z-index: 99;
  cursor: pointer;
}
.clearInp img {
  width: 48rpx;
  height: 48rpx;
}
.idAuthen {
  position: absolute;
  right: 80rpx;
  top: 25rpx;
  z-index: 99;
  cursor: pointer;
  background: rgba(0, 0, 0, 0.3);
  width: 64rpx;
  height: 32rpx;
  line-height: 32rpx;
  border-radius: 4rpx;
  text-align: center;
  color: #fff;
  font-size: 24rpx;
}
.inputBox .title {
  float: left;
}
.inputBox .name {
  float: left;
  width: 140rpx;
}
.inputBox .uni-input {
  float: right;
  width: 450rpx;
  color: rgba(0, 0, 0, 0.55);
}
.clear {
  clear: both;
  overflow: hidden;
  zoom: 1;
  height: 0px;
}
.marginTop20 {
  margin-top: 20rpx;
}
.city {
  color: rgba(0, 0, 0, 0.55);
  font-size: 28rpx;
  position: relative;
}
.rightIcon {
  position: absolute;
  right: 0rpx;
  top: 2rpx;
}
.rightIcon img {
  width: 48rpx;
  height: 48rpx;
}
.sickBox {
  display: flex;
  font-size: 28rpx;
  justify-content: space-between;
  flex-wrap: wrap;
  margin: 0 auto;
  margin-bottom: 40rpx;
}
.sickBtn {
  flex: 1;
  width: 298rpx;
  text-align: center;
  height: 72rpx;
  line-height: 72rpx;
  border: 2rpx solid #b2b2b2;
  margin: 0 34rpx;
  border-radius: 16rpx;
  position: relative;
}
.chose {
  border: 2rpx solid #f25559;
  color: #f25559;
}
.sickBtn .cur {
  position: absolute;
  right: 0;
  bottom: 0;
}
.sickBtn .cur {
  width: 46rpx;
  height: 46rpx;
}
.sickBtn .cur img {
  width: 100%;
  height: 100%;
}
.sickBtn:first-child {
  margin-left: 0;
}
.sickBtn:last-child {
  margin-right: 0;
}
.choseSickLink {
  color: #2680eb;
  font-size: 24rpx;
  margin: 20rpx 0 20rpx 0;
  text-align: right;
}
.choseSickList .spans {
  display: inline-block;
  padding: 8rpx 24rpx;
  margin: 10rpx 40rpx 10rpx 0;
  background-color: #fff;
  border: 2rpx solid #eee;
  color: rgba(0, 0, 0, 0.55);
  position: relative;
  font-size: 28rpx;
  border-radius: 8rpx;
}
.choseSickList .cur {
  background-color: #fee9e9;
  color: rgba(0, 0, 0, 0.55);
  border: 2rpx solid #fee9e9;
}
.choseSickList .blackBg {
  background-color: #fee9e9;
  border: 2rpx solid #fee9e9;
}
.clearSick {
  width: 32rpx;
  height: 32rpx;
  border: 1px solid #ccc;
  overflow: hidden;
  border-radius: 100%;
  position: absolute;
  top: -15rpx;
  right: -15rpx;
  cursor: pointer;
}
.clearSick img {
  width: 100%;
  height: 100%;
}
.uni-btn-v button {
  width: 702rpx;
  height: 88rpx;
  border: 2rpx solid rgba(0, 0, 0, 0);
  background: linear-gradient(236deg, #fe894d 0%, #ef3931 100%);
  opacity: 1;
  border-radius: 44rpx;
  color: #fff;
  font-size: 36rpx;
}
.userHistory {
  color: rgba(0, 0, 0, 0.5);
}
.userHistory .spans {
  display: inline-block;
  padding: 4rpx 24rpx;
  font-size: 28rpx;
  background-color: #f3f3f3;
  color: rgba(0, 0, 0, 0.55);
  border-radius: 28rpx;
  margin-right: 20rpx;
  margin-left: 10rpx;
  margin-bottom: 10rpx;
}
.historyCur {
  background: rgba(239, 57, 49, 0.6) !important;
  color: #fff !important;
}
.tips {
  padding-top: 20rpx;
  font-size: 26rpx;
  color: #999;
}
.content .fontSizeNone {
  font-size: inherit;
}
.marginBot20 {
  margin-bottom: 20rpx;
}
.wordTitle {
  color: rgba(0, 0, 0, 0.55);
}
.wordColorBlack {
  color: #333;
}
.tipsDesc {
  font-size: 24rpx;
  color: rgba(0, 0, 0, 0.45);
  line-height: 28rpx;
  margin-top: 20rpx;
}
</style>
<style lang="scss">
.home {
  padding-bottom: constant(safe-area-inset-bottom);
  padding-bottom: env(safe-area-inset-bottom);
}
</style>
